<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class ManageTest extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('test','',TRUE);
		$this->load->model('question','',TRUE);
	}
	
	function addnew()
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$created_user = $session_data['username'];
			
			$test_name = $this->input->post('test_name');
			
			if ($test_name == "")
			{
				$this->session->set_flashdata('error', '<p>Bạn phải điền tên đề thi!!</p>');
				redirect(site_url('tdc/test/addnew'));
			}
			$max_score = $this->input->post('max_score');
			$time = $this->input->post('time');
			$num_question = $this->input->post('num_question');
			$level = $this->input->post('level');
			$note = $this->input->post('note');
			date_default_timezone_set('Asia/Ho_Chi_Minh');
			$created_date = date("Y/m/d H:i:s", time());
			$ques_list = $this->question->get_question_4_test($num_question);
			
			if ($this->test->insert_test($test_name, $time, $level, $max_score, $num_question, $created_user, $created_date, $note, NULL) && $ques_list)
			{
				$test_id = $this->test->get_last_id();
				
				// insert 2 table test_question
				foreach ($ques_list as $row)
				{
					$question_id = $row['question_id'];
					$characters = '1234';
					
					$answer_order = '';
					while (strlen($characters) > 0) {
						$c = $characters[rand(0, strlen($characters) - 1)];
						$answer_order .= $c;
						$characters = str_replace($c, '', $characters);
					}

					$this->test->insert_question_2_test($test_id, $question_id, $answer_order);
				}	
				
				$this->session->set_flashdata('ok', '<p>Thêm đề thi thành công.</p>');
				redirect(site_url('tdc/test/addnew'));
			}
			else
			{
				$this->session->set_flashdata('error', '<p>Thêm đề thi thất bại! Sô câu hỏi không đủ!</p>');
				redirect(site_url('tdc/test/addnew'));
			}
			
			
		}
	}
	
	function mix()
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$created_user = $session_data['username'];
			$test_id = $this->input->post('test_id');
			$test_info = $this->test->get_test($test_id);
			if ($test_info)
			{
				$test_name = $test_info->test_name . ' (M) ';
				
				$max_score = $test_info->max_score;
				$time = $test_info->time;
				$num_question = $test_info->num_question;
				$level = $test_info->level;
				$note = $test_info->note;
				date_default_timezone_set('Asia/Ho_Chi_Minh');
				$created_date = date("Y/m/d H:i:s", time());
				$ques_list = $this->question->get_question_by_test($test_id);
				shuffle($ques_list);
				
				if ($this->test->insert_test($test_name, $time, $level, $max_score, $num_question, $created_user, $created_date, $note, $test_id) && $ques_list)
				{
					$mix_test_id = $this->test->get_last_id();
					
					shuffle($ques_list);
					// insert 2 table test_question
					foreach ($ques_list as $row)
					{
						$question_id = $row->question_id;
						$characters = '1234';
						
						$answer_order = '';
						while (strlen($characters) > 0) {
							$c = $characters[rand(0, strlen($characters) - 1)];
							$answer_order .= $c;
							$characters = str_replace($c, '', $characters);
						}
						
						$this->test->insert_question_2_test($mix_test_id, $question_id, $answer_order);
					}	
					
					$this->session->set_flashdata('ok', '<p>Đảo đề thi thành công. Hãy cập nhật thông tin cho đề thi mới sinh ra.</p>');
					redirect(site_url('tdc/test/edit/'. $mix_test_id));
				}
				else
				{
					$this->session->set_flashdata('error', '<p>Đảo đề thi thất bại!</p>');
					redirect(site_url('tdc/test/view/'. $test_id));
				}
				
				
			}	
		}
	}
	
	function edit()
	{
		if($this->session->userdata('logged_in'))
		{
			$session_data = $this->session->userdata('logged_in');
			$created_user = $session_data['username'];
			
			$test_id = $this->input->post('test_id');
			$test_name = $this->input->post('test_name');
			$category_id = $this->input->post('category');
			
			if ($test_name == "")
			{
				$this->session->set_flashdata('error', '<p>Bạn phải điền tên đề thi!!</p>');
				redirect(site_url('tdc/test/edit/'.$test_id));
			}
			$max_score = $this->input->post('max_score');
			$time = $this->input->post('time');
			$level = $this->input->post('level');
			$note = $this->input->post('note');
			
			if ($this->test->update_test($test_name, $time, $level, $max_score, $note, $test_id))
			{	
				
				$this->session->set_flashdata('ok', '<p>Sửa đề thi thành công.</p>');
				redirect(site_url('tdc/test/home'));
			}
			else
			{
				$this->session->set_flashdata('error', '<p>Sửa đề thi thất bại!</p>');
				redirect(site_url('tdc/test/edit/'.$test_id));
			}
			
			
		}
	}
	
	function listall()
	{
		$this->datatables->select('test_id, test_name, created_user, created_date');
		$this->datatables->from('test');
		$this->datatables->add_column('',
			'<a href="' . site_url('tdc/test/view/$1'). '"><img src="' . base_url() . 'img/i_archive.png" alt="Xem đề thi" title="Xem thông tin" /></a> &nbsp; '.
			'<a href="' . site_url('tdc/test/edit/$1'). '"><img src="' . base_url() . 'img/i_edit.png" alt="Sửa" title="Sửa" /></a> &nbsp; '.
			'<a href="' . site_url('managetest/delete/$1'). '" onclick="return del_confirm();"><img src="' . base_url() . 'img/delete-icon.png" alt="Xóa" title="Xóa" /></a>', 'test_id');
		echo $this->datatables->generate();
	}
	
	function get_question_by_test($test_id)
	{
		$this->datatables->select('content');
		$this->datatables->from('answer');
		$this->datatables->where('question_id  = ' . $question_id . ' order by `tdc_answer`.`order`');
		
		echo $this->datatables->generate();
	}
	
	function delete($test_id)
	{
		if($this->session->userdata('logged_in'))
		{

			if ($this->test->delete_test($test_id))
			{
				$this->session->set_flashdata('ok', '<p>Xóa đề thi thành công.</p>');
				redirect(site_url('tdc/test/home'));
			}
			else
			{
				$this->session->set_flashdata('error', '<p>Xóa đề thi thất bại!</p>');
				redirect(site_url('tdc/test/home'));
			}
			
		}
	}
}